<?php
/**
 * 店铺卖家控制器
 * @version 1.0
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Shop\Controller;

class SellerController extends CommonController{
    
    protected $Seller = null; //卖家事件
    
    protected function _initialize(){
        $array = array('login','verify');
        if(in_array(ACTION_NAME, $array)){
            $this->checkLogin = false;
        }
        parent::_initialize();
        $this->Seller = A('Seller','Event');
    }
    
    /**
     * 卖家登录
     */
    public function login(){
        if(IS_POST && IS_AJAX){
            $data = array('status'=>0);
            $verify = new \Think\Verify();
            if($verify->check(I('post.verify'), 'admin_login')){
                $user = I('post.username');
                $passwd = I('post.password');
                $status = $this->Seller->login($user, $passwd);
                if($status !== true){
                    $data['info'] = $status;
                }else{
                    $data['status'] = 1;
                    $data['url']   = U('Store/index');
                }
            }else{
                $data['info'] = '验证码错误';
            }
            $this->ajaxReturn($data); exit;
        }
        //已登录 直接进入后台
        if(!empty($this->sellerInfo)){
        	$this->redirect('Store/index');
        }
        //检测总平台是否设置 检测是否注册入驻商家
        if (C('STORE_IS_BIND') && !is_shop()) {
            $this->redirect('Home/Settle/index');
        }
        
        //根据登陆用户将商家信息输出
        if (C('STORE_IS_BIND')) {
            $user_info = $_SESSION['jipushop_home']['user_auth'];
            $sellerUser = M('Seller')->where('member_id='.$user_info['uid'])->find();
            $this->assign('seller',$sellerUser);
        }

        $this->display();
    }
    
    
    /**
     * 登录验证码
     */
    public function verify(){
        $verify = new \Think\Verify();
        $verify->useImgBg = false;//不使用验证码背景
        $verify->useNoise = false;//使用噪点
        $verify->length = 4;//验证码长度
        $verify->bg = array(250,250,250);//背景颜色
        ob_clean();
        $verify->entry('admin_login');
    }
    
    
    /**
     * 退出登录
     */
    public function logout(){
    	if($this->seller_id){
    		session('seller_user',null); // 清空session
    		$this->success('退出成功！', U('login'));
    	}else{
    		$this->redirect('login');
    	}
    }
    
}